package com.example.owner.lettery1.adapter

import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.owner.lettery1.R
import com.example.owner.lettery1.bean.LotteryBean
import com.example.owner.lettery1.modlus.OnItemListener
import com.example.owner.lettery1.modlus.SplitBall


class OpenLotteryAdapter(private var list: ArrayList<LotteryBean>) : RecyclerView.Adapter<OpenLotteryAdapter.LotteryHolder>() {
    var onItemListener: OnItemListener<LotteryBean>? = null

    private var dateView : TextView? = null
    private var lotteryNameView : TextView? = null
    private var issueNameView : TextView? = null
    private var recyclerView : RecyclerView? = null


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LotteryHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_lottory, parent, false)
        val holder = LotteryHolder(itemView)
        holder.onItemListener = onItemListener
        return holder
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: LotteryHolder, position: Int) {
        holder.bindData(list[position])
    }


    inner class LotteryHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var onItemListener: OnItemListener<LotteryBean>? = null

        fun bindData(bean: LotteryBean) {
            issueNameView = itemView.findViewById(R.id.issueNameView)
            lotteryNameView = itemView.findViewById(R.id.lotteryNameView)
            dateView = itemView.findViewById(R.id.dateView)
            recyclerView = itemView.findViewById(R.id.recycleView)


            lotteryNameView!!.text = bean.lotteryName
            val issue = "第" + bean.issue + "期"
            issueNameView!!.text = issue
            dateView!!.text = bean.time!!.substring(0, 10)
            val ballList = SplitBall.splitBall(bean.openNumber!!)
            val llm = LinearLayoutManager(itemView.context)
            llm.orientation = LinearLayoutManager.HORIZONTAL
           recyclerView!!.layoutManager = llm
            recyclerView!!.adapter = BallAdapter(ballList, bean.lotteryNo!!)
            recyclerView!!.isNestedScrollingEnabled = false
            itemView.setOnClickListener {
                if (onItemListener != null)
                    onItemListener!!.onClick(bean)
            }
        }
    }
}